/**
 * 数据列表页面JS
 * To change this template use File | Settings | File Templates.
 */
$(function(){
    //激活左侧导航菜单
    activeNavBar("MANAGE_OPT_ORG_INFO");
    initSelect2();
    var param = [
        {
            id: 'provinceCode',
            url: springUrl+ '/web/orgInfo/listDistrictInfo',
            code: 'districtCode',
            name: 'districtName',
            parent: ''
        },{
            id: 'cityCode',
            url: springUrl+ '/web/orgInfo/listDistrictInfo',
            code: 'districtCode',
            name: 'districtName',
            parent: 'parentDistrictCode'
        },{
            id: 'countyCode',
            url: springUrl+ '/web/orgInfo/listDistrictInfo',
            code: 'districtCode',
            name: 'districtName',
            parent: 'parentDistrictCode'
        }
    ];
    new Linkage().init(param);
    //加载数据列表
    loadGrid();

    $("#queryBtn").click(function(){
        query();
    });

    $("#resetBtn").click(function(){
        $('#searchForm')[0].reset();
        $('#searchForm').find('.select2-chosen').html('请选择');
    });

    $("#addBtn").click(function(){
        openAddPage();
    });
});

/**
 * 加载数据列表
 */
function loadGrid(){
    var gridTable = jQuery("#gridTable");
    gridTable.jqGrid({
        mtype: "POST",
        url: springUrl+"/web/orgInfo/page",
        datatype: "json",
        autowidth: true,
        height: "100%",
        colModel: [
            {name:'uniqueNo', label:'机构编号', index:'', width:50, sortable: false, resizable:false, hidden: true},
            {name:'isEnable',label:'启用状态', index:'', width:100, sortable: false, resizable:false, hidden: true},
            {name:'orgName',label:'机构名称', index:'',width:100, sortable: false, align:'left'},
            {name:'linkman',label:'联系人', index:'', width:100, sortable: false, align:'left'},
            {name:'mobileNo',label:'联系电话', index:'', width:100, sortable: false, align:'left'},
            {name:'provinceName',label:'所在省', index:'', width:100, sortable: false, align:'left'},
            {name:'cityName',label:'所在市', index:'', width:100, sortable: false, align:'left'},
            {name:'countyName',label:'所在区/县', index:'', width:100, sortable: false, align:'left'},
            {name:'createTime',label:'加入时间', index:'', width:100, sortable: false, align:'left',
                formatter:function(cellValue,options,rowObject){
                    if(cellValue !== null){
                        return moment(cellValue).format("YYYY-MM-DD");
                    }else{
                        return "";
                    }
                }
            },
            {name:'', label:'操作', index:'', width:220, fixed:true, sortable:false, resize:false, align:'center',
                formatter:function(cellValue, options, rowObject){
                    var context = "";
                    context += '<button class="mfui-btn mfui-btn-primary mfui-btn-sm" onclick="openContractConfigPage(\''+rowObject.uniqueNo+'\')" title="合同">合同</button> ';
                    if(rowObject.isEnable == 1){
                        context += '<button class="mfui-btn mfui-btn-primary mfui-btn-sm" onclick="disableByUniqueNo(\''+rowObject.uniqueNo+'\')" title="停用">停用</button> ';
                    }else{
                        context += '<button class="mfui-btn mfui-btn-primary mfui-btn-sm" onclick="enableByUniqueNo(\''+rowObject.uniqueNo+'\')" title="启用">启用</button> ';
                    }
                    context += '<button class="mfui-btn mfui-btn-primary mfui-btn-sm" onclick="openEditPage(\''+rowObject.uniqueNo+'\')" title="修改">修改</button> ';
                    // context += '<button class="mfui-btn mfui-btn-primary mfui-btn-sm" onclick="deleteByUniqueNo(\''+rowObject.uniqueNo+'\')" title="删除">删除</button>';
                    return context;
                }
            }
        ],
        rownumbers: true,//添加左侧行号
        viewrecords : true,
        rowNum: 10,
        rowList: [10,20,30],
        sortname: "create_time",
        sortorder: "desc",
        altRows: true,
        pager : "#gridPager",
        jsonReader: { //server返回Json解析设定
            root: "content", //对于json中数据列表
            page: "pageNum",
            total: "total",
            records: "records"
        },
        gridComplete: function (xhr) {
            var ids = $("#gridTable").getDataIDs();
            for(var i=0;i<ids.length;i++){
                var rowData = $("#gridTable").getRowData(ids[i]);
                if(rowData.isEnable==0){
                    $('#'+ids[i]).find("td").addClass("bg-red");
                }
            }
        }
    });
    //navButtons
    gridTable.jqGrid('navGrid',"#gridPager",
        {
            add: false,
            addicon : 'fa-plus purple',
            edit: false,
            editicon : 'fa-pencil blue',
            del: false,
            delicon : 'fa-trash red',
            view: false,
            viewicon : 'fa-search-plus orange',
            search: false,
            searchicon : 'fa-search grey',
            refresh: true,
            refreshicon : 'fa-refresh green'
        }
    );
    updatePagerIcons(this);
    resizeJqGrid("gridTable");
}

/**
 * 查询
 */
function query(){
    if($('#searchForm').trigger("validate").isValid()){
        var gridParam = {
            url: springUrl+"/web/orgInfo/page",
            postData:{
                orgNameLike : $("#orgNameLike").val(),
                provinceCode : $("#provinceCode").val(),
                cityCode : $("#cityCode").val(),
                countyCode : $("#countyCode").val(),
                isEnable : $("#isEnable").val(),
            },
            page:1
        };
        $("#gridTable").jqGrid("setGridParam", gridParam).trigger("reloadGrid");
    }
}

/**
 * 打开新增页面
 */
function openAddPage(){
    window.forward(springUrl+"/web/orgInfo/addPage");
}

/**
 * 打开修改页面
 * @param uniqueNo 机构编号
 */
function openEditPage(uniqueNo){
    window.forward(springUrl+'/web/orgInfo/editPage?uniqueNo='+uniqueNo);
}

/**
 * 打开合同配置页面
 * @param uniqueNo 机构编号
 */
function openContractConfigPage(uniqueNo){
    window.forward(springUrl+"/web/orgInfo/contractConfigPage?uniqueNo="+uniqueNo);
}

/**
 * 根据主键删除
 * @param uniqueNo 机构编号
 */
function deleteByUniqueNo(uniqueNo) {
    Alert.confirm("您确定删除该条信息？", function () {
        jQuery.ajax({
            type:"POST",
            dataType:'json',
            url: springUrl+"/web/orgInfo/deleteByUniqueNo",
            data:{
                uniqueNo:  uniqueNo
            },
            success:function (data) {
                Alert.success("执行成功！", function () {
                    $("#gridTable").trigger("reloadGrid");
                });
            },
            error : function(data){
                Alert.error(data.responseText);
            }
        });
    });
}

/**
 * 启用
 * @param uniqueNo 机构编号
 */
function enableByUniqueNo(uniqueNo){
    Alert.confirm("您确定启用该运营机构？", function () {
        jQuery.ajax({
            type:"POST",
            dataType:'json',
            url: springUrl+"/web/orgInfo/enableByUniqueNo",
            data:{
                uniqueNo:  uniqueNo
            },
            success:function (data) {
                Alert.success("执行成功！", function () {
                    $("#gridTable").trigger("reloadGrid");
                });
            },
            error : function(data){
                Alert.error(data.responseText);
            }
        });
    })
}

/**
 * 停用
 * @param uniqueNo 机构编号
 */
function disableByUniqueNo(uniqueNo){
    Alert.confirm("您确定停用该运营机构？", function () {
        jQuery.ajax({
            type:"POST",
            dataType:'json',
            url: springUrl+"/web/orgInfo/disableByUniqueNo",
            data:{
                uniqueNo:  uniqueNo
            },
            success:function (data) {
                Alert.success("执行成功！", function () {
                    $("#gridTable").trigger("reloadGrid");
                });
            },
            error : function(data){
                Alert.error(data.responseText);
            }
        });
    })
}
